const app = getApp();
import { actionsIndex } from '../../../api/api.js';
import { tips } from '../../../utils/show.js';
import { formatDateChain } from '../../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height * 2 + 40,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '活动 ', //导航栏 中间的标题
      img:"../../image/back.png"
    },
    // 活动数据
    activitys: [],
    imgUrl: app.globalData.imgUrl
  },
  
  bindViewTap: function (e) {
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status;
    if (status == 3) {
      app.showToast('活动已截止');
      return;
    } else if (status == 1) {
      app.showToast('活动未开始');
      return;
    } 
    wx.navigateTo({
      url: '/pages/index/activityList/activityList?id=' + id,
    })

  },
  
  getactionsIndex() {
    actionsIndex().then(res =>{
      if (res.code === 0) {
        let nowtime = Math.floor((new Date()).getTime()/1000);
        res.data.forEach(item => {
          // 拿到时间对比
          let begin_time = parseInt(item.begin_time);
          let end_time = parseInt(item.end_time);
          if (nowtime < begin_time) {
            item.status = 1;
            item.statusText = '未开始';
          } else if (begin_time < nowtime && nowtime < end_time) {
            item.status = 2;
            item.statusText = '报名中';
          } else {
            item.status = 3;
            item.statusText = '已截止';
          }
          item.begin_time = formatDateChain(item.begin_time);
          item.end_time = formatDateChain(item.end_time);
        });
        // console.log(res.data)
        let act = res.data;
        this.setData({
          activitys:act
        });
      } else {
        tips("数据出错");
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.getactionsIndex();
  },
 
})